// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { Bus } = require("./Bus");
const { Common } = require("./Common");
const User = require("./entity/User");
const http = require("./util/http");

cc.Class({
    extends: cc.Component,
  

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        // cocos editbox type password
        btnLogin: cc.Node,
        // edUsername: cc.Node,
        edUsername:  cc.EditBox,
        // edPassword: cc.Node,
        edPassword: cc.EditBox ,
       
        // 定义变量
// @property(cc.EditBox)
// inputEditBox: cc.EditBox = null;

// // 获取内容
// let content = this.inputEditBox.string;
// // 修改内容
// this.inputEditBox.string = "lalala";

    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {

    },

    onBtnLoginClicked(view,data){

        cc.log("data")
        cc.log(data)
        if(data=="login"){
            cc.log("login do")  
            // this.edUsername.string
            cc.log(" this.edUsername.string")  
            cc.log( this.edUsername.string)  
            cc.log(" this.edPassword.string")  
            cc.log( this.edPassword.string)  
         var userForm=   User.createNew()
         userForm.username=this.edUsername.string
         userForm.password=this.edPassword.string

            // http.post(Common.baseUrl,Common.userLogin,null,userForm,())

            http.post(Common.baseUrl, Common.userLogin, null, userForm, (err, ret) => {
                if (err) {
                    console.log(err);
                    return;
                }

                console.log(ret);
                // ret.data
                if (ret.code == Common.codeSuccess) {
                    // Bus
                    Bus.curUser = ret.data
                    cc.director.loadScene("Index");
                    return
                }
                console.log("ret.msg");
                console.log(ret.msg);
            })

            // cc.log( "this.edPassword")  
            // cc.log( this.edPassword)  
            // cc.log(" this.edUsername.string")  
            // cc.log( this.edUsername.textLa)  
            // cc.log(" this.edPassword.string")  
            // cc.log( this.edPassword.string)  
        }
        
    },

    // update (dt) {},
});
